﻿var vEntity = document.getElementById("hiddenEntity").value;
$(function () {
    if (vEntity == "CrmCustomerInfo") {
        document.getElementById("add_all").style.display = "none";
        document.getElementById("remove_all").style.display = "none";
        //移到右边
        $('#add').click(function () {
            //数据option选中的数据集合赋值给变量vSelect
            var vSelect = $("#lstBoxSubject option:selected");
            //克隆数据添加到listRight中
            $('#lstBoxResult').empty();
            vSelect.clone().appendTo("#lstBoxResult");
            //同时移除listLeft中的option
            //vSelect.remove();
        });
        //移到左边
        $('#remove').click(function () {
            $('#lstBoxResult').empty();
        });
        //双击添加
        $('#lstBoxSubject').dblclick(function () {
            //数据option选中的数据集合赋值给变量vSelect
            var vSelect = $("#lstBoxSubject option:selected");
            //克隆数据添加到listRight中
            $('#lstBoxResult').empty();
            vSelect.clone().appendTo("#lstBoxResult");
            //同时移除listLeft中的option
            //vSelect.remove();
        });
        //双击删除
        $('#lstBoxResult').dblclick(function () {
            $('#lstBoxResult').empty();
        });
    } else {
        //移到右边
        $('#add').click(function () {
            //获取选中的选项，删除并追加给对方
            $('#lstBoxSubject option:selected').appendTo('#lstBoxResult');
        });
        //移到左边
        $('#remove').click(function () {
            $('#lstBoxResult option:selected').appendTo('#lstBoxSubject');
        });
        //全部移到右边
        $('#add_all').click(function () {
            //获取全部的选项,删除并追加给对方
            $('#lstBoxSubject option').appendTo('#lstBoxResult');
        });
        //全部移到左边
        $('#remove_all').click(function () {
            $('#lstBoxResult option').appendTo('#lstBoxSubject');
        });
        //双击选项
        $('#lstBoxSubject').dblclick(function () { //绑定双击事件
            //获取全部的选项,删除并追加给对方
            $("option:selected", this).appendTo('#lstBoxResult'); //追加给对方
        });
        //双击选项
        $('#lstBoxResult').dblclick(function () {
            $("option:selected", this).appendTo('#lstBoxSubject');
        });
    }
});
//关闭弹出窗体
function closeWin() {
    YAHOO.example.container.dialog1.hide();
}
function ChangeDept() {
    var deptId = $('#SelectForDept').val();
    if (deptId == "" || deptId == null) {
        alert("请选择部门!");
    } else {
        var deptIndex = $("#SelectForDept").get(0).selectedIndex;
        document.getElementById('lstBoxSubject').length = 0;
        if (deptIndex == 0) {
            return;
        } else {
            var url = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=V_DatEmployeeForUser&id=" + deptId;
            YAHOO.util.Connect.asyncRequest('POST', url, {
                success: function (res) {
                    var inventory;
                    try {
                        inventory = res.responseText;
                        var Employee = [];
                        var obj = eval(inventory);
                        if (vEntity != "CrmCustomerInfo") {
                            if (obj.length > 0) {
                                $('#lstBoxSubject').append('<option value=22>部门所有人</option>');
                                $('#hiddenDId').val(deptId);
                            }
                        }
                        for (var i = 0; i < obj.length; i++) {
                            $('#lstBoxSubject').append('<option value=' + obj[i].UserID + '>' + obj[i].Name + '(' + obj[i].DeptName + ":" + obj[i].EmpCode +')</option>');
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        }
    }
}
//共享、分配
function Save() {
    var objsel = document.getElementById("lstBoxResult");
    var CustomerId = document.getElementById("hiddenId").value;
    var listid = "", allid = "", alldId = "",
            customOptions = "", url = "";
    if (vEntity == "CrmCustomerInfo") {  //分配
        if (objsel.options.length == 0) {
            alert("选择需要分配的人员!");
            return;
        } else {
            var vIds = "";
            if (CustomerId == "") {  //批量分配
                var vCustomerId = [];
                vCustomerId = parent._Ids;
                for (var i = 0; i < vCustomerId.length; i++) {
                    vIds += vCustomerId[i] + ",";
                }
                CustomerId = vIds;
            } else {
                CustomerId += ",";
            }
            customOptions = document.createElement("OPTION");
            allid = objsel.options[0].value;
            if (allid != "") {
                CustomerShares(CustomerId, allid);
            } else {
                alert("选择需要分配的人员");
                return;
            }
        }
    } else {  //共享
        for (var i = 0; i < objsel.options.length; i++) {
            customOptions = document.createElement("OPTION");
            if (objsel.options[i].value == "11") {  //集团所有人
                allid = objsel.options[i].value + ",";
                break;
            } else if (objsel.options[i].value == "22") {  //部门所有人
                alldId = document.getElementById("hiddenDId").value + ",22";  //部门ID
                break;
            }
            else {
                listid += objsel.options[i].value + ",";
            }
        }
        var QId = document.getElementById("hiddenId").value; //共享客户ID
        var toId = document.getElementById("hdnUid").value;
        var vType = document.getElementById("hiddenType").value; //类型 0：客户共享  1：客户分配时共享
        if (vType != "") {
            if (toId == undefined) {
                toId = "00000000-0000-0000-0000-000000000000";
            }
            if (allid != "") {
                url = "../../Extend/AjaxData/DataProcessin.aspx?type=" + vType + "&action=Save&entity=CrmCustomerShare&toId=" + toId + "&id=" + QId + "&ids=" + allid;
            } else if (alldId != "") {
                url = "../../Extend/AjaxData/DataProcessin.aspx?type=" + vType + "&action=Save&entity=CrmCustomerShare&toId=" + toId + "&id=" + QId + "&ids=" + alldId;
            } else {
                url = "../../Extend/AjaxData/DataProcessin.aspx?type=" + vType + "&action=Save&entity=CrmCustomerShare&toId=" + toId + "&id=" + QId + "&ids=" + listid;
            }
            YAHOO.util.Connect.asyncRequest('POST', url, {
                success: function (res) {
                    var inventory;
                    try {
                        inventory = res.responseText;
                        if (inventory == '1') {
                            alert("操作成功!");
                            parent.closeWin();
                            return;
                        } else {
                            alert("操作失败");
                        }
                    }
                    catch (e) {
                    }
                    finally {
                    }
                },
                failure: function (error) {
                    alert("数据错误：" + error.statusText);
                }
            }, null);
        } else {
            alert("操作失败");
            return;
        }
    }
}
var CustomerShares = function (CustomerId, UserId) {
    YAHOO.util.Connect.asyncRequest('POST', "../../Extend/AjaxData/CustomOperating.aspx?action=update&entity=CrmCustomerInfo&id=" + CustomerId + "&uid=" + UserId + "", {
        success: function (res) {
            var inventory;
            try {
                inventory = res.responseText;
                if (inventory == "1") {
                    alert("客户分配成功!");
                    parent.closeWin();
                    return;
                } else {
                    alert("分配失败");
                }
            }
            catch (e) {
            }
            finally {
            }
        },
        failure: function (error) {
            alert("数据错误：" + error.statusText);
        }
    }, null);
}

//查找按钮事件
function SearchName() {
    var vName = $("#txtSearch").val();
    if (vName != "") {
        document.getElementById('lstBoxSubject').length = 0;
        var url = "../../Extend/AjaxData/CustomOperating.aspx?action=check&entity=V_DatEmployeeForName&id=" + encodeURI(vName);
        YAHOO.util.Connect.asyncRequest('POST', url, {
            success: function (res) {
                var inventory;
                try {
                    inventory = res.responseText;
                    var Employee = [];
                    var obj = eval(inventory);
                    for (var i = 0; i < obj.length; i++) {
                        $('#lstBoxSubject').append('<option value=' + obj[i].UserID + '>' + obj[i].Name + '(' + obj[i].DeptName + ":" + obj[i].EmpCode + ')</option>');
                    }
                }
                catch (e) {
                }
                finally {
                }
            },
            failure: function (error) {
                alert("数据错误：" + error.statusText);
            }
        }, null);
    } else {
        $("#txtSearch").focus();
    }
}